#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/kthread.h>
#include <linux/wait.h>
int flag = 0;
 
static struct task_struct *test_task, *poldtask; 
wait_queue_head_t test_que;
//static DECLARE_WAIT_QUEUE_HEAD(test_que);
int test_thread(void * testPtr)
{
	printk("cj the old task state :%d \n", poldtask->__state);
	printk("cj the thread current pid : %d\n", current->pid);
	flag = 1;
	wake_up_interruptible(&test_que);
	return 0;
}
 
static int test_wait_init(void )
{
	struct wait_queue_entry data;
	int i = 0;
	//创建线程
	test_task = kthread_create(test_thread, NULL, "test_task");
	printk("the pid of test_task is %d \n", test_task->pid);
	printk("current pid is %d \n", current->pid);	
 
	init_waitqueue_head(&test_que);
	init_waitqueue_entry(&data, current);
	add_wait_queue(&test_que, &data);
	poldtask = current;
	wake_up_process(test_task); //唤醒新创建的进程
	schedule_timeout_interruptible(10*10);  //调度一下
	wait_event_interruptible(test_que, flag);  //默认flag == 0 会进入睡眠
	for(; i<10; i++)
		printk("test_wait_init end  %d \n", i );
	return 0 ;
}
 
static void test_wait_exit(void)
{
	printk("test_wait exited!\n");
}
 
 
module_init(test_wait_init);
module_exit(test_wait_exit);
MODULE_LICENSE("GPL");
